<?php
/**
 * 基础物料查询数据finder
 * @author ExBOY
 * @version 1.0
 */

class console_finder_basic_material
{
    var $detail_stock = "库存详情";
    
    function __construct()
    {
        if($_GET['ctl'] != 'admin_stock'){
            unset($this->column_edit_stock);
            unset($this->detail_stock);
        }
    }
    
    function detail_stock($product_id)
    {
        $basicMaterialSelect    = kernel::single('material_basic_select');#基础物料 ExBOY
        
        // 保存安全库存数量
        if($_POST)
        {
            $oBranchPro = app::get('ome')->model('branch_product'); 
            $branch_id = $_POST['branch_id'];
            $product_ids = $_POST['product_id'];
            $safe_store = $_POST['safe_store'];
            $is_locked = $_POST['is_locked'];
            for($k=0;$k<sizeof($branch_id);$k++) {
                $oBranchPro -> update(
                    array('safe_store'=>$safe_store[$k],'is_locked'=>$is_locked[$k]),
                    array(
                        'product_id'=>$product_ids[$k],
                        'branch_id'=>$branch_id[$k]
                    )
                );
            }
            
            $sql = 'UPDATE sdb_material_basic_material_stock SET alert_store=0 WHERE bm_id='.$product_ids[$k-1];
            kernel::database()->exec($sql);
            
            $sql = 'UPDATE sdb_material_basic_material_stock SET alert_store=999 WHERE bm_id IN 
                (
                    SELECT product_id FROM sdb_ome_branch_product 
                    WHERE product_id='.$product_ids[$k-1].' AND safe_store>(store - store_freeze + arrive_store)
                )';
            kernel::database()->exec($sql);
        }
        $render = app::get('ome')->render();
        
        $render->pagedata['pro_detail'] = $basicMaterialSelect->products_detail($product_id);
        return $render->fetch('admin/stock/detail_stock.html');
    }
    
    /*------------------------------------------------------ */
    //-- 格式化字段
    /*------------------------------------------------------ */
    #总库存
    var $column_store = '总库存';
    var $column_store_width = 80;
    var $column_store_order = 80;
    function column_store($row)
    {
        $data    = kernel::database()->selectrow("SELECT store FROM ".DB_PREFIX."material_basic_material_stock WHERE bm_id=".$row['bm_id']);
        
        return $data['store'];
    }
    
    #冻结库存
    var $column_store_freeze = '冻结库存';
    var $column_store_freeze_width = 80;
    var $column_store_freeze_order = 90;
    function column_store_freeze($row)
    {
        $data    = kernel::database()->selectrow("SELECT store_freeze FROM ".DB_PREFIX."material_basic_material_stock WHERE bm_id=".$row['bm_id']);
        
        return $data['store_freeze'];
    }
    
    #在途库存
    var $column_arrive_store='在途库存';
    var $column_arrive_store_width='60';
    var $column_arrive_store_order = 100;//排在列尾
    function column_arrive_store($row)
    {
        $sql    = "SELECT SUM(arrive_store) AS 'total' FROM ".DB_PREFIX."ome_branch_product WHERE product_id=".$row['bm_id'];
        $count  = kernel::database()->selectrow($sql);
        
        return $count['total'];
    }
    
    /*------------------------------------------------------ */
    //-- 显示行样式[粗体：highlight-row]
    //-- [加绿：list-even 加黄：selected 加红：list-warning]
    /*------------------------------------------------------ */
    public function row_style($row)
    {
        if($row[$this->col_prefix.'alert_store']==999){
            return 'list-warning';
        }
    }
}